package mn.idax.exchange.otc.notify;

import com.fasterxml.jackson.databind.ObjectMapper;
import mn.idax.exchange.otc.domain.MessageNotifyDTO;
import org.apache.poi.ss.formula.functions.T;
import org.atmosphere.config.managed.Decoder;
import org.atmosphere.config.managed.Encoder;

import java.io.IOException;

/**
 * @Auther: dison
 * @Date: 2018/8/30 15:46
 * @Description: 序列化
 */
public class JacksonEncoderDecoder implements Encoder<MessageNotifyDTO, String>, Decoder<String, MessageNotifyDTO> {

    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public String encode(MessageNotifyDTO m) {
        try {
            return this.mapper.writeValueAsString(m);
        }
        catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }

    @Override
    public MessageNotifyDTO decode(String s) {
        try {
            return this.mapper.readValue(s, MessageNotifyDTO.class);
        }
        catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }

}
